//
//  EWHView.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/11/26.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "EWHView.h"

@interface EWHView()<CAAnimationDelegate>
@property(nonatomic, strong) QMUILabel *tempLabel;
@property(nonatomic, strong) UIImageView *imageView1;
@property(nonatomic, strong) UIImageView *imageView2;
@property(nonatomic, strong) UIImageView *imageView3;
@property(nonatomic, strong) UIImageView *imageView4;
@property(nonatomic, strong) UIImageView *imageView5;

@property(nonatomic, strong) QMUIButton *leftBtn;
@property(nonatomic, strong) QMUIButton *rightBtn;

@property(nonatomic, strong) KKTimer *timer;
@property(nonatomic, strong) KKTimer *freezeTimer;

@property(nonatomic, strong) QMUILabel *tipsLabel;
@end

@implementation EWHView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorClear;
        
        self.tempLabel = [[QMUILabel alloc] init];
        self.tempLabel.font = KKFontMake(20);
        self.tempLabel.textColor = UIColorGray5;
        self.tempLabel.textAlignment = NSTextAlignmentCenter;
        [self.tempLabel sizeToFit];
        self.tempLabel.text = NSLocalizedString(@"dev_tank_temp", nil);
        [self addSubview:self.tempLabel];
        
        self.imageView1 = [[UIImageView alloc] init];
        [self addSubview:self.imageView1];
        
        self.imageView2 = [[UIImageView alloc] init];
        [self addSubview:self.imageView2];
        
        self.imageView3 = [[UIImageView alloc] init];
        self.imageView3.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.imageView3];
        
        self.imageView4 = [[UIImageView alloc] init];
        [self addSubview:self.imageView4];
        
        self.imageView5 = [[UIImageView alloc] init];
        [self addSubview:self.imageView5];
        
        self.leftBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [self.leftBtn setImage:UIImageMake(@"icon_left") forState:UIControlStateNormal];
        self.leftBtn.adjustsImageTintColorAutomatically = YES;
        [self.leftBtn addTarget:self action:@selector(leftTouchDown:) forControlEvents:UIControlEventTouchDown];
        [self.leftBtn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
        [self addSubview:self.leftBtn];
        
        self.rightBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [self.rightBtn setImage:UIImageMake(@"icon_right") forState:UIControlStateNormal];
        self.rightBtn.adjustsImageTintColorAutomatically = YES;
        [self.rightBtn addTarget:self action:@selector(rightTouchDown:) forControlEvents:UIControlEventTouchDown];
        [self.rightBtn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
        [self addSubview:self.rightBtn];
        
        self.tipsLabel = [[QMUILabel alloc] init];
        self.tipsLabel.font = KKFontMake(14);
        self.tipsLabel.textColor = UIColorGray3;
        self.tipsLabel.textAlignment = NSTextAlignmentCenter;
        self.tipsLabel.text = NSLocalizedString(@"tips_set_temperature", nil);
        [self addSubview:self.tipsLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    self.tempLabel.frame = CGRectMake(0, 0, self.qmui_width, 30);
    self.leftBtn.frame = CGRectMake(0, self.bounds.size.height / 2 - 20, 40, 40);
    self.rightBtn.frame = CGRectMake(self.qmui_width - 40, self.leftBtn.qmui_top, 40, 40);
    
    float temp = _ctrlState ? _setTemp : _currentTemp;
    NSInteger shi = (int)temp / 10;
    NSInteger ge = (int)temp % 10;
    NSInteger fen = (int)(10 * temp) % 10;
    
    NSString *name1 = [NSString stringWithFormat:@"digital_%ld",(long)shi];
    NSString *name2 = [NSString stringWithFormat:@"digital2_%ld",(long)ge];
    NSString *name3 = [NSString stringWithFormat:@"digital3_%ld",(long)fen];
    
    UIImage *image1 = UIImageMake(name1);
    UIImage *image2 = UIImageMake(name2);
    UIImage *image3 = UIImageMake(name3);
    
    CGFloat width = SCREEN_WIDTH == 320 ? 180 : 200;
    CGFloat width3 = SCREEN_WIDTH == 320 ? 50 : 60;
    CGFloat start = (self.qmui_width - width) / 2;
    CGFloat totalWidth = image1.size.width + image2.size.width;
    CGFloat scale = (width - width3 - 10) / totalWidth;
    
    self.imageView1.frame = (CGRect){ start, (self.qmui_height - image1.size.height * scale) / 2, image1.size.width * scale, image1.size.height * scale };
    self.imageView1.image = image1;
    
    self.imageView2.frame = (CGRect){ self.imageView1.qmui_right, self.imageView1.qmui_top, image2.size.width * scale, image2.size.height * scale };
    self.imageView2.image = image2;
    
    self.imageView3.frame = (CGRect){ start + width - width3, self.imageView2.qmui_bottom - width3, width3, width3 };
    self.imageView3.image = image3;
    
    CGSize dotSize = (CGSize){ 6, 4 };
    UIImage *image4 = [UIImage qmui_imageWithColor:UIColorMakeWithHex(@"811721") size:dotSize cornerRadius:dotSize.height];
    self.imageView4.frame = (CGRect){ self.imageView2.qmui_right + 2, self.imageView3.qmui_bottom - dotSize.height - 3, dotSize };
    self.imageView4.image = image4;
    
    CGSize circleSize = (CGSize){ 14, 14 };
    UIImage *image5 = [UIImage qmui_imageWithStrokeColor:UIColorMakeWithHex(@"811721") size:circleSize lineWidth:3.0f cornerRadius:circleSize.width / 2];
    self.imageView5.frame = (CGRect){ self.imageView3.qmui_right - circleSize.width, self.imageView1.qmui_top + 3, circleSize };
    self.imageView5.image = image5;
    
    self.tipsLabel.frame = (CGRect){ 10, self.imageView1.qmui_bottom + 10, self.qmui_width - 20, 20 };
    if (!self.tipsLabel.isHidden && ![self.tipsLabel.layer animationForKey:@"kFlashAnimation"]) {
        CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
        flash.fromValue = @1.0f;
        flash.toValue = @0.0f;
        flash.autoreverses = YES;
        flash.duration = 1;
        flash.repeatCount = 3;
        flash.removedOnCompletion = YES;
        flash.fillMode = kCAFillModeForwards;
        flash.delegate = self;
        flash.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [self.tipsLabel.layer addAnimation:flash forKey:@"kFlashAnimation"];
    }
    
    [self bringSubviewToFront:self.leftBtn];
    [self bringSubviewToFront:self.rightBtn];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        self.tipsLabel.hidden = YES;
    }
}

-(void)setCurrentTemp:(CGFloat)currentTemp {
    _currentTemp = currentTemp;
    [self setNeedsLayout];
}

-(void)setSetTemp:(CGFloat)setTemp {
    if (setTemp >= _minTemp && setTemp <= _maxTemp) {
        _setTemp = setTemp;
        [self setNeedsLayout];
    }
    if (_timer && _timer.interval == 1.0) {
        _timer.interval = 0.5f;
    }
}

-(void)setCtrlState:(NSInteger)ctrlState {
    _ctrlState = ctrlState;
    if (ctrlState > 0) {
        self.tempLabel.text = NSLocalizedString(@"dev_set_temp", nil);
    } else {
        self.tempLabel.text = NSLocalizedString(@"dev_tank_temp", nil);
    }
    [self setNeedsLayout];
}

-(void)leftTouchDown:(id)sender {
    [self touchDown:-0.5f];
}

-(void)rightTouchDown:(id)sender {
    [self touchDown:0.5f];
}

-(void)touchDown:(CGFloat)value {
    if (_freezeTimer && _freezeTimer.isValid) {
        [_freezeTimer invalidate];
        _freezeTimer = nil;
    }
    if(_ctrlState == 0) {
        self.ctrlState = 1;
        [_delegate targetTempWillChange:self];
    } else {
        self.ctrlState = 2;
        self.setTemp += value;
    }
    
    WS(ws);
    _timer = [KKTimer every:1.0f task:^(NSUInteger times) {
        ws.ctrlState = 2;
        ws.setTemp += value;
    }];
}

-(void)touchUp:(id)sender {
    if (_timer && _timer.isValid) {
        [_timer invalidate];
        _timer = nil;
    }
    if (_ctrlState == 2) {
        [_delegate whView:self targetTempChanged:_setTemp];
    }
    
    WS(ws);
    _freezeTimer = [KKTimer once:5.0f task:^(NSUInteger times) {
        LOG(@"set isCtrl NO");
        ws.ctrlState = 0;
    }];
}
@end
